#-*- encoding: gb2312 -*-
import os, sys, string
import poplib
import ConfigParser
import threading
import re
import imaplib
import socket

threads = 10

def pop_auth(host, username, password, port, ssl):
	try:
		if ssl:
			pp = poplib.POP3_SSL(host, port)
		else:
			pp = poplib.POP3(host, port)
		pp.set_debuglevel(0)
		pp.user(username)
		s = pp.pass_(password)
		pp.stat()
	except poplib.error_proto:
	#	print "check wrong!"
		pp.quit()
		return 0
	except socket.error:
	#	print "connection error"
		return -1
	#print "check ok!"	
	pp.quit()
	return 1

def imap_auth(host, username, password, port, ssl):
	try:
		if ssl:
			imap = imaplib.IMAP4_SSL(host, port)
		else:
			imap = imaplib.IMAP4(host, port)
	except socket.error:
	#	print "connection error"
		return -1

	try:
		imap.login(username, password)
	except:
	#	print "check wrong!"
		imap.logout()
		return 0
	#print "check ok!"	
	imap.logout()
	return 1


def mail_vaild(mail):
	pattern = re.compile('\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*')
	match = pattern.match(mail)
	if match:
		return 1
	return 0

def outfile(outstr):
	print outstr
	global inputname
	global flock
	flock.acquire()
	outname = inputname + '.out.txt'
	fp = open(outname, 'a') 
	fp.write(outstr + '\n')
	fp.close()
	flock.release()

if len(sys.argv) < 2:
	print "usage: %s maillist" % sys.argv[0]
	sys.exit(-1)

f = open(sys.argv[1], 'r')
mails = f.readlines()
mails.reverse()
#print mails
f.close()
inputname = sys.argv[1]

cf = ConfigParser.ConfigParser() 
cf.read("config.ini") 

s = cf.sections()  
#print 'section:', s  

class mythread(threading.Thread):

	def __init__(self,threadname):
		threading.Thread.__init__(self,name=threadname)

	def run(self):
		while 1:
			lock.acquire()
			global mails
			if len(mails) == 0:
				lock.release()
				print 'thread stop'
				break
			up = mails.pop().strip()
			#print up
			lock.release()

			try:
				mail = re.split("\s+",up)[0]
				password = re.split("\s+",up)[1]
				if mail_vaild(mail) and password:
					#print mail
					#print password
					mail_class = mail.split('@')[1]
					mail_class = mail_class.lower()
					if(mail_class.find('hotmail') >= 0):
						mail_class = 'hotmail.com'
					if(mail_class.find('yahoo') >= 0):
						mail_class = 'yahoo.com'
					if(mail_class.find('googlemail') >= 0):
						mail_class = 'gmail.com'
					#print mail_class
					host = cf.get(mail_class, 'host')
					port = cf.getint(mail_class, 'port')
					ssl = cf.getint(mail_class, 'ssl')
					mailtype = cf.get(mail_class, 'type')

					if mailtype == 'pop':
						pop_ret = pop_auth(host, mail, password, port, ssl)
						if pop_ret == 1:
							outfile(up + '\tright')
						elif pop_ret == -1:
							outfile(up + '\tconnectionError')
						else:
							outfile(up + '\twrong')
					elif mailtype == 'imap':
						imap_ret = imap_auth(host, mail, password, port, ssl)
						if imap_ret == 1:
							outfile(up + '\tright')
						elif imap_ret == -1:
							outfile(up + '\tconnectionError')
						else:
							outfile(up + '\twrong')
					else:
						outfile(up + '\tunknowProtocol')
				else:
					outfile(up + '\temailError')

			except IndexError:
				#print "unvaild line"
				outfile(up + '\tformatError')
			except ConfigParser.Error:
				outfile(up + '\tunkownMail')


lock=threading.RLock()
flock = threading.Lock()
t1=[]
for i in range(threads):
	t=mythread(str(i))
	t1.append(t)

for i in t1:
	i.start()

